//
//  DiscoverViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/18.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "DiscoverViewController.h"
#import "DiscoverTableViewCell.h"
#import "ActivityDetailViewController.h"
#import "DiscoverModel.h"
@interface DiscoverViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UIView * topView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * leftView;
@property (nonatomic, assign)BOOL isPopular;
@property (nonatomic, strong)DiscoverModel * model;
@property (nonatomic, assign)int currentPage;
@property (nonatomic, strong)NSMutableArray * dataArr;
@property (nonatomic, strong)NSString * diccoverIdStr;
@end

@implementation DiscoverViewController(Networking)
- (void)getDiscoverData{
    NSDictionary * dic = @{
                           @"page":@(self.currentPage),
                           @"row":@(6),
                           @"rule":self.isPopular?@(1):@(2),
                           };
    WS(ws)
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/activity/activity") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [ws.tableView.mj_footer endRefreshing];
        [ws.tableView.mj_header endRefreshing];
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000){
                if (ws.currentPage == 1) {
                    [ws.dataArr removeAllObjects];
                }
                ws.model = [DiscoverModel yy_modelWithDictionary:obj];
                [ws.dataArr addObjectsFromArray:ws.model.data];
                [ws.tableView reloadData];
            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}

@end

@implementation DiscoverViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBar.translucent = NO;

    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    window.backgroundColor = getColor(mainColor);
    
    [self getDiscoverData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.topView];
    [self.view addSubview:self.tableView];
    
    self.dataArr = [NSMutableArray array];
    _isPopular = YES;
    _currentPage = 1;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100 / 2)];
        _topView.backgroundColor = [UIColor whiteColor];
        
        //右按钮
        UIButton * popularityBtn = [[UIButton alloc] init];
        [popularityBtn setTitle:@"人气" forState:UIControlStateNormal];
        popularityBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [popularityBtn setTitleColor:getColor(@"6d6d6d") forState:UIControlStateNormal];
        [popularityBtn addTarget:self action:@selector(clickMove:) forControlEvents:UIControlEventTouchUpInside];
        popularityBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, _topView.height);
        [_topView addSubview:popularityBtn];
        
        int leftViewX = 38;
        int lineViewW = SCREEN_WIDTH / 2 - 2 * leftViewX;
        int lineViewH = 3;
        int lineViewY = _topView.bottom - 3;
        self.leftView = [[UIView alloc] init];
        self.leftView.backgroundColor = getColor(@"ffbb02");
        self.leftView.frame = CGRectMake(leftViewX, lineViewY, lineViewW, lineViewH);
        [self.topView addSubview:self.leftView];
        
        //左按钮
        UIButton * timeBtn = [[UIButton alloc] init];
        [timeBtn setTitle:@"时间" forState:UIControlStateNormal];
        timeBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [timeBtn setTitleColor:getColor(@"6d6d6d") forState:UIControlStateNormal];
        timeBtn.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, _topView.height);
        [timeBtn addTarget:self action:@selector(clickMove:) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:timeBtn];
        
        //中线
        int middleViewY = 9;
        int middleViewH = _topView.height - 2 * middleViewY;
        UIView * middleView = [[UIView alloc] init];
        middleView.backgroundColor = getColor(@"d2d2d2");
        middleView.frame = CGRectMake(SCREEN_WIDTH / 2, middleViewY, 1, middleViewH);
        [_topView addSubview:middleView];
        
    }return _topView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.topView.height, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - self.topView.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = getColor(@"e5e5e5");
        
        WS(ws)
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            ws.currentPage = 1;
            [self getDiscoverData];
        }];
        
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            ws.currentPage ++;
            [self getDiscoverData];
        }];
        [self.tableView.mj_header beginRefreshing];
    }return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellID = @"cell";
    [self.tableView registerClass:[DiscoverTableViewCell class] forCellReuseIdentifier:cellID];
    DiscoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {
        cell = [[DiscoverTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.modelData = self.dataArr[indexPath.row];
    _diccoverIdStr = cell.modelData.discoverId;
//    NSLog(@"%@", cell.modelData.discoverId);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220 / HEIGHT_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([UserModel isSaveLogin]) {
        ActivityDetailViewController * activityDetailVC = [[ActivityDetailViewController alloc] init];
        DiscoverTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        _diccoverIdStr = cell.modelData.discoverId;
        activityDetailVC.activityId = [_diccoverIdStr intValue];
        NSLog(@"发现活动ID%@",_diccoverIdStr);
        [self.navigationController pushViewController:activityDetailVC animated:YES];
    }else{
        [AppHelper showLogin];
    }    
//    static NSString *CellIdentifier = @"cell";
//    DiscoverTableViewCell *cell =(DiscoverTableViewCell*) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

- (void)clickMove:(UIButton *)sender
{
    if (sender.frame.origin.x + 1 > SCREEN_WIDTH / 2) {
        _isPopular = NO;
    }else{
        _isPopular = YES;
    }
    [self.tableView.mj_header beginRefreshing];
    WS(ws)
    [UIView animateWithDuration:0.3 animations:^{
        ws.leftView.x = sender.x + 38;
    }];
}

@end
